package ipdesk.model.report.brand.action;

import ipdesk.model.common.dao.CommonDao;
import ipdesk.model.report.brand.bean.BrandInfoBean;
import ipdesk.model.report.brand.dao.BrandInfoDao;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.itf.dao.ECGDao;
import ecg.util.FileService;
import ecg.util.SqlMapLocator;
import ecg.util.StringUtils;
import ecg.util.Time;

public class BrandInfoRegAction extends ActionSupport implements Preparable, ModelDriven<BrandInfoBean>
{
	private BrandInfoBean brandInfoBean;
	
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		String returnFlag = "SUCCESS";
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.brand");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		brandInfoBean.setPath_name(basePath);
		brandInfoBean.setRegTime(regTime);
	//	System.out.println("******"+brandInfoBean.getDeskCode());
	//	brandInfoBean.setEmblem(StringUtils.nl2br(brandInfoBean.getEmblem()));
	//	brandInfoBean.setSimilarBrand(StringUtils.nl2br(brandInfoBean.getSimilarBrand()));
	//	brandInfoBean.setComment(StringUtils.nl2br(brandInfoBean.getComment()));
		
		// desk
		if(!brandInfoBean.getDc1().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc1()); }
		if(!brandInfoBean.getDc2().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc2()); }
		if(!brandInfoBean.getDc3().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc3()); }
		if(!brandInfoBean.getDc4().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc4()); }
		if(!brandInfoBean.getDc5().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc5()); }
		if(!brandInfoBean.getDc6().equals("")) { brandInfoBean.setDeskCode(brandInfoBean.getDc6()); }
		
		System.out.println("******"+brandInfoBean.getDeskCode());
		int companySeqCount;
		if(brandInfoBean.getEvaluationResult() == null){
			brandInfoBean.setEvaluationResult(" ");
		}
		System.out.println("******"+brandInfoBean.getEvaluationResult());
		if(brandInfoBean.getEvaluationResult().equals("a")){
			// 회사당 4개까지 등록확인
			companySeqCount = CommonDao.getInstance().companySeqCount(brandInfoBean.getCompanySeq());
		}else {
			companySeqCount = 0;
		}
		
		if(companySeqCount > 4) {
			System.out.println("====4개이상");
			returnFlag = "COUNTFAIL";
		} else if(companySeqCount<4) {
			System.out.println("====4개이하");
			int lastBrandSeq = BrandInfoDao.getInstance().brandInfoReg(brandInfoBean);
			
			// 마지막으로 insert된 consult_seq 값을 빈 객체에 저장.		
			brandInfoBean.setBrandSeq(lastBrandSeq);
			
			if(lastBrandSeq != 0 && brandInfoBean.getUpload() != null) {
				String uploadFileName = brandInfoBean.getUploadFileName();
				String realFileName = fileService.saveFile(brandInfoBean.getUpload(), totalPath, uploadFileName);
				brandInfoBean.setUploadFileName(realFileName);
				brandInfoBean.setFile_category("FLE11");
				
				BrandInfoDao.getInstance().insertFileData(brandInfoBean);
			}
			System.out.println("파일이름1 : " + brandInfoBean.getUploadAFFileName());
			if(lastBrandSeq != 0 && brandInfoBean.getUploadAF() != null) {
				String uploadFileName = brandInfoBean.getUploadAFFileName();
				System.out.println("파일이름 : " + brandInfoBean.getUploadAFFileName());
				String realFileName = fileService.saveFile(brandInfoBean.getUploadAF(), totalPath, uploadFileName);
				brandInfoBean.setUploadFileName(realFileName);
				brandInfoBean.setFile_category("FLE12");
				
				BrandInfoDao.getInstance().insertFileData(brandInfoBean);
			}
			
			if(lastBrandSeq != 0 && brandInfoBean.getUploadESC() != null) {
				String uploadFileName = brandInfoBean.getUploadESCFileName();
				String realFileName = fileService.saveFile(brandInfoBean.getUploadESC(), totalPath, uploadFileName);
				brandInfoBean.setUploadFileName(realFileName);
				brandInfoBean.setFile_category("FLE13");
				
				BrandInfoDao.getInstance().insertFileData(brandInfoBean);
			}
			
			returnFlag = "SUCCESS";
		}else{
			returnFlag = "COUNTFAIL";
		}
		
		return returnFlag;
	}
	
	public void prepare() throws Exception
	{
		brandInfoBean = new BrandInfoBean();
	}
	
	public BrandInfoBean getModel()
	{
		return brandInfoBean;
	}
	
	/*public void validate()
	{
		String contentType = brandInfoBean.getUploadContentType();
		long fileSize = 0;
		
		if(brandInfoBean.getUpload() != null)
		{
			fileSize = brandInfoBean.getUpload().length();
		}
		
		if(!fileService.contentTypeValidate(contentType))
		{
			addFieldError("upload", "업로드할 수 없는 파일형식입니다");
			return;
		}
		
		if(fileSize > Long.parseLong(getText("upload.size")))
		{
			addFieldError("upload", "업로드 용량을 초과하였습니다.");
			return;
		}
		
	}*/
}
